<?php

namespace App\Exceptions;

use App\Utils\Logs;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Str;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  \Exception  $exception
     * @return void
     */
    public function report(Exception $exception)
    {
//        // 判断是否开启错误日志.
//        if (Str::contains(config('myLog.apart_level'), 'error')) {
//            Logs::error($exception);
//        }
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        if($exception instanceof BaseException){
            $this->code = $exception->code;
            $this->msg = $exception->msg;
            $this->errCode = $exception->errCode;
        }else{
            if(env('APP_DEBUG')){
                return parent::render($request,$exception);
            }else{
                $this->code = 400;
                $this->msg = $exception->getMessage();
                $this->ErrorLog($exception);
            }
        }
        $json_data = [
            'code'      =>  (int)$this->code,
            'msg'       =>  $this->msg,
            'data'      =>  null,
        ];
        var_dump($json_data);die();
        return response($json_data);
//        return parent::render($request, $exception);
    }


    public function ErrorLog($exception)
    {
        Logs::error($exception);
    }
}
